const log4 = require('../utils/log4')()
const lodash = require('lodash')

async function logger(ctx, next) {
	ctx.logger = log4
	ctx.startTime = Date.now()

	await next()

	if (ctx.logIgnore === true) return

	let req = JSON.stringify({
		query: ctx.query,
		params: ctx.params,
		body: ctx.request.body
  })


	let res = JSON.stringify(lodash.omit(ctx.body, ctx.logIgnore))

	log4.info(`${ctx.path} ${Date.now() - ctx.startTime}ms req:${req} res:${res}`)
}

module.exports = logger